﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MazeModel
{
    public class EnemyWallhugger : MazeObject
    {
        private int heading;                    // Direction of heading
        private int moveDelay;                  // Amount of frames between moves (lower number = more speedy, 0 = max speed)
        private int[] surroundingBlocks;        // this things' surrounding blocks as they are perceived
        private int moveCount;                  // Amount of frames since last move
        

        public EnemyWallhugger(int i, int j)
            : base(i, j)
        {
            this.moveDelay = 1;
            this.heading = 1;
            this.surroundingBlocks = new int[5];
        }

        // Makes this enemy react and make its next move
        // up,down,left,right indicate the values of the cells above,below,left,right of it
        // pposi = player position i  (which line)   -
        // pposj = player position j  (which column) |
        public void react(int up, int down, int left, int right, int pposi, int pposj)
        {
            // Do nothing except increase moveCount if it's not time to move yet
            if (moveCount < moveDelay)
            {
                moveCount++;
                return;
            }
            
            moveCount = 0;
            surroundingBlocks[1] = up;
            surroundingBlocks[2] = down;
            surroundingBlocks[3] = left;
            surroundingBlocks[4] = right;

            // Only calculate new heading if subpos is 0,0 since otherwise it's moving between blocks
            if ((this.subpos[0] == 0) && (this.subpos[1] == 0))
            {
                switch (heading)
                {
                    case 1:
                        if (surroundingBlocks[2] != 1)
                            this.heading = 2;
                        if (surroundingBlocks[3] != 1)
                            this.heading = 3;
                        if (surroundingBlocks[1] != 1)
                            this.heading = 1;
                        if (surroundingBlocks[4] != 1)
                            this.heading = 4;
                        break;
                    case 2:
                        if (surroundingBlocks[1] != 1)
                            this.heading = 1;
                        if (surroundingBlocks[4] != 1)
                            this.heading = 4;
                        if (surroundingBlocks[2] != 1)
                            this.heading = 2;
                        if (surroundingBlocks[3] != 1)
                            this.heading = 3;
                        break;
                    case 3:
                        if (surroundingBlocks[4] != 1)
                            this.heading = 4;
                        if (surroundingBlocks[2] != 1)
                            this.heading = 2;
                        if (surroundingBlocks[3] != 1)
                            this.heading = 3;
                        if (surroundingBlocks[1] != 1)
                            this.heading = 1;
                        break;
                    case 4:
                        if (surroundingBlocks[3] != 1)
                            this.heading = 3;
                        if (surroundingBlocks[1] != 1)
                            this.heading = 1;
                        if (surroundingBlocks[4] != 1)
                            this.heading = 4;
                        if (surroundingBlocks[2] != 1)
                            this.heading = 2;
                        break;
                    default:
                        break;
                }
            }    
            // Move in direction "heading"
            this.move(surroundingBlocks[heading], heading);
    
        }

        // Returns the direction to try next if the current one is blocked
        // This will make enemy tend to walk in 
      //  private int nextClockwiseHeading(int h)
      //  { 
       //     switch (h)
      //          case 1: return 4;
      //          case 2: return 
       // }
    }

}
